package org.wu.framework.authorization.platform.application.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.wu.framework.authorization.platform.application.SysMenuApplication;
import org.wu.framework.authorization.platform.application.assembler.SysMenuDTOAssembler;
import org.wu.framework.authorization.platform.application.command.SysMenuCommand;
import org.wu.framework.authorization.platform.application.dto.SysMenuTreeDTO;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenu;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenuRepository;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl;
import org.wu.framework.lazy.orm.web.plus.stereotype.LazyApplication;
import org.wu.framework.translation.data.dictionary.NormalConvertMapper;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyApplicationImpl
 **/
@LazyApplication
public class SysMenuApplicationImpl implements SysMenuApplication {

    @Autowired
    SysMenuRepository sysMenuRepository;

    /**
     * describe 新增
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> save(SysMenuCommand sysMenuCommand) {
        SysMenu sysMenu = SysMenuDTOAssembler.toMenu(sysMenuCommand);
        return sysMenuRepository.story(sysMenu);
    }

    /**
     * describe 更新
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> update(SysMenuCommand sysMenuCommand) {
        SysMenu sysMenu = SysMenuDTOAssembler.toMenu(sysMenuCommand);
        return sysMenuRepository.story(sysMenu);
    }

    /**
     * describe 查询单个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> findOne(SysMenuCommand sysMenuCommand) {
        SysMenu sysMenu = SysMenuDTOAssembler.toMenu(sysMenuCommand);
        return sysMenuRepository.findOne(sysMenu);
    }

    /**
     * describe 查询多个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysMenu>> findList(SysMenuCommand sysMenuCommand) {
        SysMenu sysMenu = SysMenuDTOAssembler.toMenu(sysMenuCommand);
        return sysMenuRepository.findList(sysMenu);
    }

    /**
     * 查询菜单树
     *
     * @param sysMenuCommand
     * @return
     */
    @NormalConvertMapper
    @Override
    public Result<List<SysMenuTreeDTO>> findTree(SysMenuCommand sysMenuCommand) {
        sysMenuCommand.setType(null);
        Result<List<SysMenu>> result = findList(sysMenuCommand);
        if(result.hasSuccessData()){
            List<SysMenu> sysMenuList = result.getData();
           List<SysMenuTreeDTO> sysMenuTreeDTOList = SysMenuDTOAssembler.treeFromMenu(sysMenuList);
           return ResultFactory.successOf(sysMenuTreeDTOList);
        }
        return ResultFactory.successOf();
    }

    /**
     * describe 删除
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> delete(SysMenuCommand sysMenuCommand) {
        SysMenu sysMenu = SysMenuDTOAssembler.toMenu(sysMenuCommand);
        return sysMenuRepository.delete(sysMenu);
    }

    /**
     * 批量存储 增量更新
     *
     * @param sysMenuCommandList
     */
    @Override
    public Result upsert(List<SysMenuCommand> sysMenuCommandList) {
        List<SysMenu> sysMenuList = sysMenuCommandList.stream().map(SysMenuDTOAssembler::toMenu).collect(Collectors.toList());
        return sysMenuRepository.batchStory(sysMenuList);
    }

    /**
     * 删除所有菜单
     *
     * @param ids
     * @return
     */
    @Override
    public Result<SysMenu> deleteByIds(List<Integer> ids) {
        return sysMenuRepository.deleteByIds(ids);
    }
}